package com.mz.mattress.client.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Coupon;
import com.mz.mattress.api.entity.Member;
import com.mz.mattress.api.entity.MemberCoupon;
import com.mz.mattress.api.enums.CouponSendTypeEnum;
import com.mz.mattress.api.enums.CouponStatusEnum;
import com.mz.mattress.client.mapper.CouponMapper;
import com.mz.mattress.client.mapper.MemberCouponMapper;
import com.mz.mattress.client.mapper.MemberMapper;
import com.mz.mattress.client.service.CouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CouponServiceImpl implements CouponService {

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private MemberCouponMapper memberCouponMapper;
    
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public void sendCoupon(Long memberId, CouponSendTypeEnum couponSendTypeEnum) {
        List<Coupon> coupons = couponMapper.selectList(new LambdaQueryWrapper<Coupon>().eq(Coupon::getSendType, couponSendTypeEnum.getValue()).eq(Coupon::getStatus, 1));
        doSendCouponToMember(memberId, coupons);
    }

    private void doSendCouponToMember(Long memberId, List<Coupon> coupons) {
        for (Coupon coupon : coupons) {
            sendCoupon(memberId, coupon, null);
        }
    }

    @Override
    public Result sendCoupon(Long memberId, Integer couponId, Integer count) {
        Coupon coupon = couponMapper.selectById(couponId);
        if (coupon == null) {
            log.error("not_find_coupon,couponId=" + couponId);
            return Result.fail();
        }
        return sendCoupon(memberId, coupon, count);
    }

    @Override
    public Result sendCoupon(Long memberId, Coupon coupon, Integer count) {

        if (count == null) {
            if (coupon.getCount() != null) {
                count = coupon.getCount();
            } else {
                //默认发放一张
                count = 1;
            }
        }

        MemberCoupon memberCoupon = new MemberCoupon();
        memberCoupon.setCouponId(coupon.getId());
        memberCoupon.setMemberId(memberId);
        memberCoupon.setStatus(CouponStatusEnum.UNUSED.getValue());
        memberCoupon.setName(coupon.getName());

        memberCoupon.setFullMoney(coupon.getFullMoney());
        memberCoupon.setMinusMoney(coupon.getMinusMoney());

        //暂时没时间限制
        // 设置优惠券  有效期开始和结束时间
//        Integer days = coupon.getDays();
//        if (days == null) {
//            memberCoupon.setStartTime(coupon.getStartTime());
//            memberCoupon.setEndTime(coupon.getEndTime());
//        } else {
//            memberCoupon.setStartTime(DateUtil.getNowDateTime());
//            LocalDateTime now = LocalDateTime.now().plusDays(days);
//            LocalDateTime endTime = LocalDateTime.of(now.toLocalDate(), LocalTime.MAX);
//            // 当天最大时间
//            memberCoupon.setEndTime(endTime);
//        }

        for (int i = 0; i < count; i++) {
            memberCoupon.setId(null);
            memberCouponMapper.insert(memberCoupon);
        }

        return Result.ok();
    }

    @Override
    public void backCoupon(Long orderId) {
        memberCouponMapper.cleanMemberCouponByOrderId(orderId);
    }

    @Override
    public Result sendCouponByMobile(String mobile, Integer couponId, Integer count) {

        Member member = memberMapper.selectByMobile(mobile);
        if (member == null) {
            return Result.fail();
        }

        return sendCoupon(member.getId(), couponId, count);
    }

    @Override
    public Coupon getById(Integer id) {
        return couponMapper.selectById(id);
    }

//    @Override
//    public void sendCoupon() {
//
//        log.info("start_sendCoupon");
//        List<Long> ids = memberMapper.selectAllIds();
//
//        for (Long id : ids) {
//            this.sendCoupon(id, CouponSendTypeEnum.NEW_REGISTER);
//        }
//
//        log.info("end_sendCoupon");
//    }
}
